package dp.observer.weather;

import java.util.ArrayList;
import java.util.Random;

import dp.observer.interfaces.Observer;
import dp.observer.interfaces.Subject;


public class WeatherDataCollector implements Subject{

	// --------------- 模式相关 ---------------
	
	private ArrayList<Observer> observerList;
	
	public WeatherDataCollector() {
		observerList = new ArrayList<Observer>();
	}
	
	public void notifyObservers() {
		for(Observer ob : observerList)
		{
			ob.update(this);
		}
	}

	public void registerObserver(Observer ob) {
		observerList.add(ob);
	}

	public void removeObserver(Observer ob) {
		//int i = observerList.indexOf(ob);
		//observerList.remove(i);
		observerList.remove(ob);
	}
	
	// --------------- 业务相关 ---------------
	private float temprature;
	private float humidity;
	private float pressure;
	
	public void collectWeatherData() {
		Random rand = new Random();
		temprature = -30.0f + 70*rand.nextFloat();
		humidity = rand.nextFloat();
		pressure = rand.nextFloat();
		weatherDataCollected();
	}
	
	public void weatherDataCollected(){
		notifyObservers();
	}

	public float getHumidity() {
		return humidity;
	}

	public float getPressure() {
		return pressure;
	}

	public float getTemprature() {
		return temprature;
	}
}
